<!--
 * @Description: 
 * @Version: 1.0
 * @Autor: younghxp
 * @Date: 2021-11-08 11:01:15
 * @LastEditors: younghxp
 * @LastEditTime: 2021-11-23 11:22:02
-->
<template>
  <div class="container" ref="mainCon">
    <div class="mainCon">
      <el-row
        v-if="$store.state.userType === 'MONITOR'"
        class="toolbar searchInputWidth"
        style="padding-bottom: 0; padding-top: 24px !important"
      >
        <el-form :inline="true" :model="filterField" ref="searchConditions">
          <el-col :lg="8" :xl="6">
            <el-form-item label="项目名称" class="row-padding-bottom">
              <el-input
                v-model="filterField.projectName"
                placeholder="请输入项目名称"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :lg="8" :xl="6">
            <el-form-item label="区域" class="row-padding-bottom">
              <el-cascader
                ref="manageArea"
                v-model="areaId"
                :append-to-body="false"
                :options="areasOptions"
                @change="getAreaId"
                clearable
                filterable
                :props="{ checkStrictly: true }"
              ></el-cascader>
            </el-form-item>
          </el-col>
          <template v-if="advanced">
            <el-col :lg="8" :xl="6">
              <el-form-item label="行业" class="row-padding-bottom">
                <el-select
                  v-model="filterField.industryId"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in industryOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="投资类型" class="row-padding-bottom">
                <el-select
                  v-model="filterField.investment"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in investmentOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="项目状态" class="row-padding-bottom">
                <el-select
                  v-model="filterField.projectStatus"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in statusOptions"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="创建时间" class="row-padding-bottom">
                <timeSelection
                  :startTime="filterField.createDateStart"
                  :endTime="filterField.createDateEnd"
                  @getStartTimeValue="getStartTimeValue"
                  @getEndTimeValue="getEndTimeValue"
                ></timeSelection>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="订单状态" class="row-padding-bottom">
                <el-select
                  v-model="filterField.status"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in orderStatusOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="保函类型" class="row-padding-bottom">
                <el-select
                  v-model="filterField.insureType"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="item in typeOptions"
                    :key="item.code"
                    :label="item.name"
                    :value="item.code"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="承保机构" class="row-padding-bottom">
                <el-select
                  v-model="filterField.insureOrg"
                  filterable
                  placeholder="请选择"
                  clearable
                  :popper-append-to-body="false"
                >
                  <el-option
                    v-for="(item, index) in guaranteeInstitutionOptions"
                    :key="index"
                    :label="item.name"
                    :value="item.name"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :xl="6">
              <el-form-item label="订单号" class="row-padding-bottom">
                <el-input
                  v-model="filterField.insureId"
                  placeholder="请输入订单号"
                  clearable
                ></el-input>
              </el-form-item>
            </el-col>
          </template>
          <el-form-item label="">
            <a
              @click="toggleAdvanced"
              style="margin-left: 40px; color: #409eff; cursor: pointer"
              class="btnColor"
            >
              {{ advanced ? '收起' : '展开' }}
              <i
                :class="advanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
              ></i>
            </a>
            <el-button
              type="primary"
              icon="icon iconfont iconchazhao1"
              @click="searchList"
              class="searchBtn"
            >
              <span>搜索</span>
            </el-button>
          </el-form-item>
        </el-form>
      </el-row>
      <div class="spacing" v-if="$store.state.userType === 'MONITOR'"></div>
      <div
        ref="tableBox"
        :class="hiddleToolBar ? 'tableBoxCur tableBox' : 'tableBoxCur'"
      >
        <el-table
          ref="tableBox1"
          @sort-change="sortChange"
          :max-height="tableHeight"
          :data="tableList"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
          highlight-current-row
          v-loading="loading"
          stripe
          style="border: 1px solid #dfe6ec"
        >
          <el-table-column
            prop="insureId"
            label="订单号"
            align="center"
            :formatter="formatTd"
            min-width="220"
          ></el-table-column>
          <el-table-column label="项目名称" min-width="200">
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.projectName"
                :columnName="'projectName'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            v-if="$store.state.userType === 'MONITOR'"
            label="区域"
            :formatter="formatTd"
            min-width="160"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.areaDetailDesc"
                :columnName="'areaDetailDesc'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            v-if="$store.state.userType === 'MONITOR'"
            prop="projectStatusDesc"
            label="项目状态"
            align="center"
            :formatter="formatTd"
            min-width="120"
          ></el-table-column>
          <el-table-column
            prop="insureTypeDesc"
            label="保函类型"
            align="center"
            :formatter="formatTd"
            min-width="140"
          ></el-table-column>
          <el-table-column
            prop="insureOrgDesc"
            label="承保机构"
            :formatter="formatTd"
            width="230"
          >
            <template slot-scope="scope">
              <copyText
                :tableIndex="scope.$index"
                :copyTextName="scope.row.insureOrgDesc"
                :columnName="'insureOrgDesc'"
              ></copyText>
            </template>
          </el-table-column>
          <el-table-column
            label="担保金额"
            align="center"
            prop="insureAmount"
            sortable="custom"
            :formatter="formatTd"
            min-width="160"
          >
            <template slot-scope="scope">
              {{ scope.row.insureAmount | splitNumber }}
            </template>
          </el-table-column>
          <el-table-column
            label="担保期限"
            align="center"
            :formatter="formatTd"
            min-width="200"
          >
            <template slot-scope="scope">
              {{ scope.row.startDate + ' - ' + scope.row.lastDate }}
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="创建时间"
            align="center"
            sortable="custom"
            :formatter="formatTd"
            min-width="200"
          ></el-table-column>
          <el-table-column
            prop="statusDesc"
            label="订单状态"
            align="center"
            :formatter="formatTd"
            min-width="150"
          ></el-table-column>
          <el-table-column
            label="操作"
            align="left"
            fixed="right"
            min-width="235"
          >
            <template slot-scope="scope">
              <template>
                <el-link
                  type="primary"
                  v-hasBtn="'insuranceOrder:detail'"
                  @click="toDetail(scope.row, 'detail')"
                  >详情</el-link
                >
              </template>
            </template>
          </el-table-column>
          <div slot="empty" v-show="isShow">
            <emptyTable emptyText="暂无相关信息"></emptyTable>
          </div>
        </el-table>
        <Pagination
          ref="pagination"
          @getList="getTableList"
          :filterField="filterField"
          :requestUrl="requestUrl"
          @getpageNo="getpageNo"
          @getpageSize="getpageSize"
        ></Pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { mapState } from 'vuex'
export default {
  name: 'checkPendingList',
  data() {
    return {
      advanced: false,
      areaId: '', //区域
      requestUrl: 'api-d/depositinsinsure/expand/auditListForUser',
      filterField: {
        projectId: '',
        projectName: '',
        areaId: '',
        industryId: '',
        investment: '',
        projectStatus: '',
        createDateStart: '',
        createDateEnd: '',
        status: '',
        insureOrgStr: '',
        insureId: '',
        insureType: '',
        serverArea: this.$store.state.areaInfoArr.areaId, // 服务部署区域
      },
      pageSize: 20,
      pageNo: 1,
      tableList: [],
      orderStatusOptions: [],
      typeOptions: [],
      guaranteeInstitutionOptions: [],
      isSaveLoading: false,
      loading: false,
      isShow: false,
      hiddleToolBar: false,
      tableHeight: null,
    }
  },
  computed: {
    ...mapState([
      'investmentOptions',
      'statusOptions',
      'areasOptions',
      'industryOptions',
    ]),
  },
  created() {
    if (
      this.$store.state.userType === 'PROJECT_ADMINISTRATOR' ||
      this.$store.state.userType === 'LABOR_ADMINISTRATOR' ||
      this.$store.state.userType === 'FENBAO_MANAGER'
    ) {
      this.filterField.projectId = this.$store.state.projectId
    }
    this.getGuaranteeInstitutionOptions()
    this.getOrderStatusOptions()
    this.getSelectData()
    this.$nextTick(() => {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    })
  },
  mounted() {
    window.addEventListener('scroll', this.handleScroll, true)
    let that = this
    this.$nextTick(function () {
      let tableHeightCur = this.$refs.tableBox1.$el.clientHeight
      let tableTop = this.$refs.tableBox.getBoundingClientRect().top
      if (tableHeightCur + tableTop > window.innerHeight) {
        this.tableHeight =
          this.$refs.mainCon.getBoundingClientRect().height - 86
      } else {
        document.getElementsByClassName('tableBoxCur').length &&
          (document.getElementsByClassName('tableBoxCur')[0].style.height =
            window.innerHeight - tableTop - 28 + 'px')
      }

      window.onresize = function () {
        if (!that.$refs.tableBox1) {
          return
        }
        let tableHeightCur = that.$refs.tableBox1.$el.clientHeight
        let tableTop = that.$refs.tableBox.getBoundingClientRect().top
        if (tableHeightCur + tableTop > window.innerHeight) {
          that.tableHeight =
            that.$refs.mainCon.getBoundingClientRect().height - 86
        } else {
          document.getElementsByClassName('tableBoxCur').length &&
            (document.getElementsByClassName('tableBoxCur')[0].style.height =
              window.innerHeight - tableTop - 40 + 'px')
        }
      }
    })
  },
  activated() {
    window.addEventListener('scroll', this.handleScroll, true)
    this.$nextTick(function () {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
      this.$refs.tableBox1.doLayout()
    })
  },
  deactivated() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  beforeDestroy() {
    let that = this
    window.removeEventListener('scroll', that.handleScroll, true)
  },
  methods: {
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    getpageSize(val) {
      this.pageSize = val
    },
    getpageNo(val) {
      this.pageNo = val
    },
    // 保函类型
    getSelectData() {
      this.axios({
        method: 'post',
        url: 'api-bs/base/dicitem/findbytype',
        data: {
          type: 'DEPOSIT_INSURE_TYPE',
        },
      })
        .then((response) => {
          this.typeOptions = response.data.result
        })
        .catch((response) => {})
    },
    // 订单状态下拉
    getOrderStatusOptions() {
      this.axios({
        method: 'post',
        url: 'api-d/depositinsinsure/expand/getinsurestatus',
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.orderStatusOptions = response.data.result
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    // 担保机构下拉
    getGuaranteeInstitutionOptions() {
      this.axios({
        method: 'post',
        url: 'api-d/insconfiginsurers/findByAllInsurers',
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.guaranteeInstitutionOptions = response.data.result
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    // 列表
    getTableList(data) {
      this.tableList = data
      this.loading = false
      if (this.tableList.length < 1) {
        this.isShow = true
      }
    },
    // 详情
    toDetail({ status, id } = row, handleType) {
      const query = {
        id,
        status,
      }
      switch (handleType) {
        case 'detail':
          Object.assign(query, { step: '2' })
          break
        case 'update':
          Object.assign(query, { step: '1', type: 'update', status: '' })
          break
        case 'renew':
          Object.assign(query, { step: '1', type: 'renew', status: '' })
          break
        default:
          break
      }
      this.$router.push({
        path: '/home/newInsurance/insuranceOrderProgress',
        name: 'insuranceOrderProgress',
        query,
      })
      this.$store.commit('CHANGE_SLIDER_NAME', {
        oldUrl: '/home/newInsurance/insuranceOrder',
        newUrl: this.$router.currentRoute.fullPath,
      })
    },
    // 取消订单
    cancleOrder(scope) {
      scope._self.$refs[`popover-${scope.$index}`].doClose()
      const { id } = scope.row
      return this.axios({
        method: 'post',
        url: 'api-d/depositinsinsure/expand/cancel',
        data: {
          id,
        },
      })
        .then((response) => {
          if (!response.data.hasError) {
            this.$message.success('操作成功')
            this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
          } else {
            this.$message.error(response.data.errorMsg)
          }
        })
        .catch((error) => {
          if (error.response) {
            this.$message.error(error.response.data)
          }
        })
    },
    searchList() {
      this.$refs.pagination.getTableListData(this.pageNo, this.pageSize)
    },
    //获取选中的区域最后一级id
    getAreaId() {
      var areaId = this.areaId[this.areaId.length - 1]
      this.filterField.areaId = areaId
    },
    getStartTimeValue(val) {
      if (val) {
        this.filterField.createDateStart = val
      } else {
        this.filterField.createDateStart = ''
      }
    },
    getEndTimeValue(val) {
      if (val) {
        this.filterField.createDateEnd = val
      } else {
        this.filterField.createDateEnd = ''
      }
    },
    handleScroll(e) {
      this.$nextTick(function () {
        if (
          this.$refs.tableBox &&
          this.$refs.tableBox.getBoundingClientRect().top < 128
        ) {
          // this.tableHeight = this.$refs.mainCon.getBoundingClientRect().height - 84
          this.tableHeight = window.innerHeight - 216

          if (document.getElementsByClassName('tableBoxCur').length) {
            document.getElementsByClassName('tableBoxCur')[0].style.height =
              null
          }
          this.hiddleToolBar = true
        } else {
          this.hiddleToolBar = false
        }
      })
    },
  },
}
</script>
<style lang="less" scoped>
.mainCon {
  padding-top: 0;
  min-height: 600px;
}
@import '../../../static/css/filterArea.less';
</style>
